#!/usr/bin/env python

import pygtk
pygtk.require("2.0")
import gtk, gobject
from location import Location
from share import Share
from validator import *

class Share_Window:
    def __init__(self,locations,share=None):
        self.locations = locations
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)
        self.window.connect("delete_event", self.delete_event)
        
        if share:
            self.window.set_title("Share Settings")
            
            self.share = Share(share.name)
            self.share.location = share.location
            self.share.name = share.name
            self.share.location = share.location
            self.share.automount = share.automount
            self.share.service = share.service
            self.share.share = share.share
            self.share.mount_point = share.mount_point
            self.share.workgroup = share.workgroup
            self.share.autentication = share.autentication
            self.share.protocol = share.protocol
            
            self.window.add(self.build_share_VBox())
            
            self.TB_share_name.set_text(share.name)
            self.Ddl_Location.set_active(self.get_location(self.share.location.name))
            self.TB_domain.set_text(share.workgroup)
            self.CB_Automount.set_active(share.automount)
            self.TB_share_path.set_text(share.service)
            self.TB_mount_point.set_text(share.mount_point)
#            if len(share.autentication) == 2:
#                self.TB_Login.set_text(share.Login[0])
#                self.TB_Password.set_text(share.Login[1])
        else:
            self.window.set_title("Add Share")
            self.share = Share("")
            self.window.add(self.build_share_VBox())
            
        self.window.show_all()
    
    def get_location(self,location_name):
        self.Ddl_Location.set_active(0)
        while self.Ddl_Location.get_active_text() != location_name:
            self.Ddl_Location.set_active(self.Ddl_Location.get_active() + 1)
        return self.Ddl_Location.get_active()
        
    def build_share_VBox(self):
        def Build_HBox(widgets):
            if len(widgets)>0:
                HBox = gtk.HBox(True, 5)
                for widget in widgets:
                    HBox.pack_start(widget, True, True, 10)
            return HBox
        
        #Share name
        self.Share_VBox = gtk.VBox(False, 5)
        self.Lbl_share_name = gtk.Label("Share Name :")
        self.TB_share_name = gtk.Entry(max=0)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_share_name,self.TB_share_name]),False,False,10)
        
        #Share location
        self.Lbl_Location = gtk.Label("Parent :")
        self.Ddl_Location = gtk.combo_box_new_text()
        for item in self.locations:
            self.Ddl_Location.append_text(item)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_Location,self.Ddl_Location]),False,False,10)
        
        #Share Path
        self.Lbl_share_path = gtk.Label("Share Path :")
        self.TB_share_path = gtk.Entry(max=0)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_share_path,self.TB_share_path]),False,False,10)
        #Mount Point
        self.Lbl_mount_point = gtk.Label("Mount Point :")
        self.TB_mount_point = gtk.Entry(max=0)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_mount_point,self.TB_mount_point]),False,False,10)
        #Dommain Name
        self.Lbl_domain = gtk.Label("Domain :")
        self.TB_domain = gtk.Entry(max=0)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_domain,self.TB_domain]),False,False,10)
        #Autentication
        self.CB_Autentication = gtk.CheckButton("Autentication")
        self.CB_Autentication.connect("toggled", self.CB_Autentication_toggled_callback)
        self.Share_VBox.pack_start(Build_HBox([self.CB_Autentication]),False,False,10)
        #Login
        self.Lbl_Login = gtk.Label("Login :")
        self.TB_Login = gtk.Entry(max=0)
        self.TB_Login.set_editable(False)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_Login,self.TB_Login]),False,False,10)
        #Password
        self.Lbl_Password = gtk.Label("Password :")
        self.TB_Password = gtk.Entry(max=0)
        self.TB_Password.set_visibility(False)
        self.TB_Password.set_editable(False)
        self.Share_VBox.pack_start(Build_HBox([self.Lbl_Password,self.TB_Password]),False,False,10)
        
        #Autostart
        self.CB_Automount = gtk.CheckButton("Automount")
        self.Share_VBox.pack_start(Build_HBox([self.CB_Automount]),False,False,10)
        #submit button
        self.Btn_Submit = gtk.Button("Submit")
        self.Btn_Submit.connect("clicked", self.submit)
        self.Share_VBox.pack_start(Build_HBox([self.Btn_Submit]),False,False,10)
        return self.Share_VBox
        
    def CB_Autentication_toggled_callback(self,widget):
        if widget.get_active() == True:
            self.TB_Login.set_editable(True)
            self.TB_Password.set_editable(True)
        elif widget.get_active() == False:
            self.TB_Login.set_editable(False)
            self.TB_Password.set_editable(False)
            self.TB_Login.set_text("")
            self.TB_Password.set_text("")
    
    def Is_Valid(self):
        validator = Validator(self.locations)
        validator.add(self.TB_share_name,"REGEX_NAME",self.Lbl_share_name)
        validator.add(self.TB_share_path,"REGEX_PATH",self.Lbl_share_path)
        validator.add(self.TB_mount_point,"REGEX_MOUNT_POINT",self.Lbl_mount_point)
        validator.add(self.TB_domain,"REGEX_DOMAIN",self.Lbl_domain)
        if self.CB_Autentication.get_active():
            validator.add(self.TB_Login,"REGEX_LOGIN",self.Lbl_Login)
            validator.add(self.TB_Password,"REGEX_PASSWORD",self.Lbl_Password)
        if validator.isvalid():
            location = self.Ddl_Location.get_model().get_value(self.Ddl_Location.get_model().get_iter(self.Ddl_Location.get_active()),0)
            if ( not self.TB_share_name.get_text() in self.locations[location].shares ) or ( self.share.name == self.TB_share_name.get_text() and self.share.location.name == location) :
                return True
        return False

    def submit(self,widget):
        if self.Is_Valid():
            self.password = self.TB_Password.get_text()
            self.window.destroy()
                    
    def delete_event(self, widget, event):
        """delete_event is call when the close button is pressed"""  
        self.share = None
        return False #False call the destroy signal True don't
    
    def destroy(self, widget):
       gtk.main_quit()
    
    def main(self):
        gtk.main()
        if self.share != None:
            self.share.name = self.TB_share_name.get_text()
            self.share.location = self.locations[self.Ddl_Location.get_model().get_value(self.Ddl_Location.get_model().get_iter(self.Ddl_Location.get_active()),0)]
            self.share.domain = self.TB_domain.get_text()
            self.share.automount = self.CB_Automount.get_active()
            self.share.service = self.TB_share_path.get_text()
            self.share.mount_point = self.TB_mount_point.get_text()
            self.share.Autentication = self.TB_Login.get_text(),self.password
        return self.share
    
if __name__ == '__main__':
    share = Share("Public")
    locations = {"Location1" : Location("Location1"),"Location2" : Location("Location2"),"Location3" : Location("Location3")}
    share.location = locations["Location3"]
    share.mount_point = "/mnt/cifs-server/public"
    share.service = "//cifs-server/Public"
    
    window = Share_Window(locations,share)
    new_share = window.main()
    if new_share != None:
        print "Name : " + new_share.name
        print "Location : " + new_share.location.name
        print "Server : " + new_share.server
        print "Service : " + new_share.service
        print "Mount_point :"  + new_share.mount_point
        print "Workgroup : " + new_share.workgroup        
#        print "Login : " + new_share.Login[0]
#        print "Password : " + new_share.Login[1]
        print "Automount : " + str(new_share.automount)